<?php
require_once '../entity/Watcher.php';
require_once '../entity/NotificationType.php';
require_once 'NotificationDao.php';
require_once 'DaoUtils.php';
class WatcherDao extends DaoUtils {
	public function getCountWatcherFrom($id) {
		$query = " SELECT COUNT(*) AS COUNT ";
		$query .= "FROM " . Watcher::getTableName() . " WHERE id_masker_from = ".$id;
		
		$record = $this -> selectRecord($query);
		return $record;
	}

	public function getCountWatcherTo($id) {
		$query = " SELECT COUNT(*) AS COUNT ";
		$query .= "FROM " . Watcher::getTableName() . " WHERE id_masker_to = ".$id;
		
		$record = $this -> selectRecord($query);
		return $record;
	}
	
	public function checkFollow($idMaskerLogged, $idMaskerProfile) {
		$query = " SELECT COUNT(*) AS COUNT ";
		$query .= "FROM " . Watcher::getTableName() . " WHERE id_masker_from = ".$idMaskerLogged." and id_masker_to = ".$idMaskerProfile;
		
		$record = $this -> selectRecord($query);
		return $record;
	}
	
	public function followUnfollow($idMaskerLogged, $idMaskerProfile, $toFollow) {
		$notificationDao = new NotificationDao();
		if ($toFollow == "true") {
			$query = "INSERT INTO ".Watcher::getTableName()." (id_masker_from, id_masker_to) VALUES (".$idMaskerLogged.",".$idMaskerProfile.")";
			$executeValue = $this->insert($query);
			
			$notificationDao->insertNotification($idMaskerLogged, $idMaskerProfile, NotificationType::$follow);
		} else {
			$query = "DELETE FROM ".Watcher::getTableName(). " WHERE id_masker_from = ".$idMaskerLogged." and id_masker_to = ".$idMaskerProfile;
			$executeValue = $this->delete($query);
			
			$notificationDao->insertNotification($idMaskerLogged, $idMaskerProfile, NotificationType::$unfollow);
		}
		
		return $executeValue;
	}
	
	public function getWatchers($nameMasker) {
		$query = "select * from masker where id IN (select id_masker_to from watcher inner join masker on id_masker_from = id where name = '".$nameMasker."')";
		return $this->selectRecords($query);
	}
	
	
	public function getFollowers($nameMasker) {
		$query = "select * from masker where id IN (select id_masker_from from watcher inner join masker on id_masker_to = id where name = '".$nameMasker."')";
		return $this->selectRecords($query);
	}
}
?>